#include <Driver.h>

#include <mysql/mysql.h>

namespace{
    bool is_alive(const CExpert::DBChecker::conn_params& cp){
        my_init();
        MYSQL handle;
        mysql_init(&handle);
        int port = atoi(cp.port.c_str());
        unsigned int connect_timeout = 1;
        unsigned int read_timeout = 1;
        unsigned int write_timeout = 1;
        mysql_options(&handle, MYSQL_OPT_CONNECT_TIMEOUT, reinterpret_cast<const char*>(&connect_timeout));
        mysql_options(&handle, MYSQL_OPT_READ_TIMEOUT, reinterpret_cast<const char*>(&read_timeout));
        mysql_options(&handle, MYSQL_OPT_WRITE_TIMEOUT, reinterpret_cast<const char*>(&write_timeout));
        MYSQL* result = mysql_real_connect(&handle, cp.host.c_str(), cp.username.c_str(), cp.passwd.c_str(), cp.db.c_str(), port, NULL, 0);
        mysql_close(&handle);
        return result;
    }
}
void mysql_init(){
    static int mysql_init_v = CExpert::register_driver("mysql", &is_alive);
    (void)mysql_init_v;
}
